package vip.youngboy.freejump.service.form;

import com.alibaba.fastjson.JSONObject;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import vip.youngboy.freejump.constant.FormType;
import vip.youngboy.freejump.domain.form.FormDef;
import vip.youngboy.freejump.domain.form.StartFormData;
import vip.youngboy.freejump.domain.form.TaskFormData;
import vip.youngboy.freejump.domain.process.BpmDefExt;
import vip.youngboy.freejump.domain.process.ProcessStartInfo;
import vip.youngboy.freejump.persist.bpmext.BpmExtPersistence;
import vip.youngboy.freejump.persist.form.FormPersistence;
import vip.youngboy.freejump.service.bus.BusDataService;

import java.util.List;

@Component
public class DefaultFormService implements FormService {

    @Autowired
    private ProcessEngineConfiguration processEngineConfiguration;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private BpmExtPersistence bpmExtPersistence;

    @Autowired
    private FormPersistence formPersistence;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private BusDataService busDataService;

    @Override
    public StartFormData getStartFormData(String procDefId) {
        StartFormData startFormData = new StartFormData();
        //设置流程定义信息
        ProcessDefinition processDefinition = getProcessDefinition(procDefId);
        startFormData.setProcessDefinition(processDefinition);
        //查询开始表单信息
        BpmDefExt bpmDefExt = bpmExtPersistence.getBpmDefExt(procDefId);
        String formKey = bpmDefExt.getProcessSetting().getForm();
        List<FormDef> formDefs = formPersistence.getFormDefByFormKey(formKey);
        //设置流程启动表单
        for (FormDef formDef : formDefs) {
            String formType = formDef.getFormType();
            if (formType.equals(FormType.PC.getKey())) {
                startFormData.setPcStartForm(formDef.getFormBody());
            } else if (formType.equals(FormType.MOBILE.getKey())) {
                startFormData.setMobileStartForm(formDef.getFormBody());
            }
        }
        return startFormData;
    }

    protected ProcessDefinition getProcessDefinition(String procDefId) {
        return repositoryService.createProcessDefinitionQuery().processDefinitionId(procDefId).singleResult();
    }


    @Override
    public ProcessInstance submitStartFormData(ProcessStartInfo processStartInfo) {
        String defKey = processStartInfo.getDefKey();
        String defId = processStartInfo.getDefId();
        JSONObject busData = processStartInfo.getData();
        if (defId == null && defKey == null) {
            throw new IllegalArgumentException("defId 和 defKey不能同时为空");
        }

        ProcessInstance processInstance = null;
        if (defId != null) {
            //使用流程定义id启动
            processInstance = runtimeService.startProcessInstanceById(defId, processStartInfo.getVariables());
        } else if (defKey != null) {
            processInstance = runtimeService.startProcessInstanceByKey(defKey, processStartInfo.getVariables());
        }

        //TODO 解析data对象，持久化业务对象
        String bussinessId = busDataService.parseAndSaveBusData(busData);

        //TODO 持久化流程与业务的关系

        return processInstance;
    }

    @Override
    public TaskFormData getTaskFormData(String taskId) {
        return null;
    }
}
